package com.xyrl.common.util;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串工具类
 */
public class StringUtil {
    //用于生成随机码
    private static final String VERIFY_CODES = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";

    public static String mobileHide(String mobile) {
        if (ObjectUtil.isNotEmpty(mobile)) {
            mobile = mobile.substring(0, 3) + "****" + mobile.substring(mobile.length() - 4);
        }
        return mobile;
    }

    public static String formatStr(String data, Object... args) {
        String result = String.format(data, args);
        return result;
    }

    public static String formates(String data, Object... args) {
        int count = 0;
        while (data.indexOf("{}") >= 0) {
            if (count >= args.length) {
                break;
            }
            data = data.substring(0, data.indexOf("{}")) + args[count] + data.substring(data.indexOf("{}") + 2);
            count++;
        }
        return data;
    }

    /**
     * @Description: 把数组转换为一个用逗号分隔的字符串 ，以便于用in+String 查询
     */
    public static String converToString(String[] ig) {
        String str = "";
        if (ig != null && ig.length > 0) {
            for (int i = 0; i < ig.length; i++) {
                str += ig[i] + ",";
            }
        }
        str = str.substring(0, str.length() - 1);
        return str;
    }

    /**
     * @Description: 把list转换为一个用逗号分隔的字符串
     */
    public static String listToString(List list) {
        StringBuilder sb = new StringBuilder();
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                if (i < list.size() - 1) {
                    sb.append(list.get(i) + ",");
                } else {
                    sb.append(list.get(i));
                }
            }
        }
        return sb.toString();
    }

    /**
     * @Description: 把list转换为一个用逗号分隔的字符串
     */
    public static String setToString(Set<String> set) {
        StringBuilder sb = new StringBuilder();
        if (ObjectUtil.isNotEmpty(set)) {
            for (String key : set) {
                if (ObjectUtil.isEmpty(sb)) {
                    sb.append(key);
                } else {
                    sb.append("," + key);
                }
            }
        }
        return sb.toString();
    }

    public static String formate(String... strs) {
        StringBuffer rtn = new StringBuffer("");
        for (String str : strs) {
            if (ObjectUtil.isEmpty(rtn)) {
                rtn.append(str);
            } else {
                if (ObjectUtil.isNotEmpty(str)) {
                    rtn.append("_" + str);
                }
            }
        }
        return rtn.toString();
    }

    /**
     * 取得url中的参数
     *
     * @param url 请求的url
     * @return
     */
    public static Map<String, String> urlParams(String url) {
        if (ObjectUtil.isNotEmpty(url)) {
            if (url.indexOf("?") >= 0) {
                url = url.substring(url.indexOf("?") + 1);
                String[] params = url.split("&");
                Map<String, String> rtn = new HashMap<String, String>();
                for (String param : params) {
                    if (param.indexOf("=") >= 0) {
                        String[] params_ = param.split("=");
                        if (params_.length >= 2) {
                            rtn.put(params_[0], params_[1]);
                        }

                    }
                }
                return rtn;
            }
        }
        return null;
    }

    public static String randomCode(int length) {
        //使用到Algerian字体，系统里没有的话需要安装字体，字体只显示大写，去掉了1,0,i,o几个容易混淆的字符
        Random rand = new Random(System.currentTimeMillis());
        StringBuilder verifyCode = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            verifyCode.append(VERIFY_CODES.charAt(rand.nextInt(VERIFY_CODES.length() - 1)));
        }
        return verifyCode.toString();
    }

    public static String randomNumberCode(int length) {
        Random random = new Random();
        String rtn = "";
        for (int i = 0; i < length; i++) {
            rtn += random.nextInt(10);
        }
        return rtn;
    }

    /**
     * 将exception 转换成string
     *
     * @param e
     * @return
     */
    public static String getErrorInfoFromException(Exception e) {
        Writer writer = new StringWriter();
        PrintWriter printWriter = new PrintWriter(writer);
        e.printStackTrace(printWriter);
        Throwable cause = e.getCause();
        while (cause != null) {
            cause.printStackTrace(printWriter);
            cause = cause.getCause();
        }
        printWriter.close();
        String result = writer.toString();
        return result;
    }

    /**
     * 字符串连接
     *
     * @param strs
     * @return
     */
    public static String concat(String... strs) {
        StringBuffer rtn = new StringBuffer("");
        if (ObjectUtil.isNotEmpty(strs)) {
            for (String str : strs) {
                rtn.append(str);
            }
        }
        return rtn.toString();
    }

    /**
     * 截取字值长度
     *
     * @param str 字符串
     * @param len 长度
     * @return
     */
    public static String subStr(String str, int len) {
        StringBuffer rtn = new StringBuffer();
        if (ObjectUtil.isNotEmpty(str)) {
            int l = str.getBytes().length;
            if (l <= len) {
                rtn.append(str);
            } else {
                char[] ar = str.toCharArray();//char数组
                for (int i = 0; i < ar.length; i++) {
                    int curLen = rtn.toString().getBytes().length + String.valueOf(ar[i]).getBytes().length;
                    if (curLen > len) {
                        break;
                    }
                    rtn.append(ar[i]);
                }
            }
        }
        return rtn.toString();
    }

    /**
     * 判断两个字符串是否相等
     *
     * @param str
     * @param source
     * @return
     */
    public static boolean equals(String str, String source) {
        //传入字符均为空 则返认为相同
        if (ObjectUtil.isEmpty(str) && ObjectUtil.isEmpty(source)) {
            return true;
        }
        //两字符均不为空则进行比对 否则一个为空一个不为空则返回不相等
        if (ObjectUtil.isNotEmpty(str) && ObjectUtil.isNotEmpty(source)) {
            return str.equalsIgnoreCase(source);
        } else {
            return false;
        }
    }

    public static String subStr(String str) {
        return subStr(str, 32);
    }

    /**
     * 校验登录帐号
     *
     * @param str
     * @return
     */
    public static boolean checkLoginName(String str) {
        //字符数字校验 [a-zA-Z_]{1,}[0-9]{0,}@(([a-zA-z0-9]-*){1,}\.){1,3}[a-zA-z\-]{1,}
        String regEx = "^[A-Za-z0-9]+$";
        // 编译正则表达式
        Pattern pattern = Pattern.compile(regEx);
        Matcher matcher = pattern.matcher(str);
        // 字符串是否与正则表达式相匹配
        boolean rs = matcher.matches();
        return rs;
    }

    public static void main(String[] args) {
        //生成目录按 年/月/日存放
        Calendar calendar = Calendar.getInstance();
        System.out.println(calendar.get(Calendar.YEAR));
        System.out.println(calendar.get(Calendar.MONTH) + 1);
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
    }
}
